from typing import List, Optional
from uuid import UUID
from datetime import datetime
from pydantic import BaseModel, Field


class DocumentCategoryBase(BaseModel):
    """文档分类基础模型"""
    name: str = Field(..., description="分类名称", max_length=100)
    icon: str = Field(default="📄", description="分类图标")
    color: str = Field(default="#666666", description="分类颜色")
    description: Optional[str] = Field(None, description="分类描述")
    parent_id: Optional[UUID] = Field(None, description="父分类ID")
    sort_order: int = Field(default=0, description="排序顺序")


class DocumentCategoryCreate(DocumentCategoryBase):
    """创建文档分类请求模型"""
    pass


class DocumentCategoryUpdate(BaseModel):
    """更新文档分类请求模型"""
    name: Optional[str] = Field(None, description="分类名称", max_length=100)
    icon: Optional[str] = Field(None, description="分类图标")
    color: Optional[str] = Field(None, description="分类颜色")
    description: Optional[str] = Field(None, description="分类描述")
    parent_id: Optional[UUID] = Field(None, description="父分类ID")
    sort_order: Optional[int] = Field(None, description="排序顺序")


class DocumentCategoryResponse(DocumentCategoryBase):
    """文档分类响应模型"""
    id: UUID
    is_system: bool = Field(description="是否系统预设分类")
    document_count: int = Field(default=0, description="包含的文档数量")
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class DocumentCategoryTreeResponse(DocumentCategoryResponse):
    """文档分类树形结构响应模型"""
    children: List['DocumentCategoryTreeResponse'] = Field(default=[], description="子分类列表")

    class Config:
        from_attributes = True


class DocumentCategoryStatistics(BaseModel):
    """文档分类统计信息"""
    category_id: UUID
    category_name: str
    document_count: int
    total_size: int = Field(description="总文件大小(字节)")


class CategoryMoveRequest(BaseModel):
    """分类移动请求模型"""
    target_category_id: Optional[UUID] = Field(None, description="目标分类ID，None表示移动到根目录")


# 解决前向引用问题
DocumentCategoryTreeResponse.model_rebuild()
